using System;
using System.Data;
using System.Text;
using System.Collections;
using System.Security.Cryptography;
using Indeco.G24.Entities;
using Indeco.G24.Services;
using Indeco.G24.Data;
using System.Web.Security;

namespace Indeco.G24.Web
{
    public class SiteIdentity : System.Security.Principal.IIdentity
    {
        private Guid userID;
        private string userName;
        private string passWord;
        private string eMail;

        public string AuthenticationType
        {
            get
            {
                return "Custom Authentication";
            }
            set
            { 
                // Do Nothing
            }
        }
        public bool IsAuthenticated
        {
            get
            {
                return true;
            }
        }
        public string Name
        {
            get
            {
                return userName;
            }
        }
        public SiteIdentity(string currentUserName)
        {
            //DMS.DataService.User dataUser = new DMS.DataService.User();
            //DataRow userRow = dataUser.Retrieve(currentUserName);

            //fullName = (string)userRow["FULLNAME"];
            //passWord=(string)userRow["PASSWORD"];
            //userDesc = userRow["DESCRIPTION"] == System.DBNull.Value ? "" : (string)userRow["DESCRIPTION"];//row.Quantity == System.BDNull ? 0 : row.Quantity
            //userName = currentUserName;
            //userLang = Convert.ToInt32(userRow["LANGUAGE"]);
            //userID = Convert.ToInt32(userRow["USERID"].ToString());
            MembershipUser dataUser = Membership.GetUser(currentUserName);
            userID = new Guid(dataUser.ProviderUserKey.ToString());
            userName = dataUser.UserName;
            passWord = dataUser.GetPassword();
            eMail = dataUser.Email;

        }
        public SiteIdentity(Guid currentUserID)
        {
            //DMS.DataService.User dataUser = new DMS.DataService.User();
            //DataRow userRow = dataUser.Retrieve(currentUserID);

            //fullName = (string)userRow["FULLNAME"];
            //passWord = (string)userRow["PASSWORD"];
            //userDesc = userRow["DESCRIPTION"] == System.DBNull.Value ? "" : (string)userRow["DESCRIPTION"];//row.Quantity == System.BDNull ? 0 : row.Quantity
            //userName=(string)userRow["USERNAME"];
            //userID = currentUserID;
            //userLang = Convert.ToInt32(userRow["LANGUAGE"]);
            MembershipUser dataUser = Membership.GetUser(currentUserID);
            userID = new Guid(dataUser.ProviderUserKey.ToString());
            userName = dataUser.UserName;
            passWord = dataUser.GetPassword();
            eMail = dataUser.Email;
        }
        public int TestPassword(string password)
        {
            // At some point, we may have a more complex way of encrypting or storing the passwords
            // so by supplying this procedure, we can simply replace its contents to move password
            // comparison to the database (as we've done below) or somewhere else (e.g. another
            // web service, etc).

            //UnicodeEncoding encoding = new UnicodeEncoding();
            //byte[] hashBytes = encoding.GetBytes(password);

            //SHA1 sha1 = new SHA1CryptoServiceProvider();
            //byte[] bytePassword = sha1.ComputeHash(hashBytes);
            //string cryptPassword = Convert.ToBase64String(bytePassword);
            //DMS.DataService.User dataUser = new DMS.DataService.User();

            //return dataUser.TestPassword(userID, cryptPassword);
            MembershipUser dataUser = Membership.GetUser(userID);
            //dataUser.
            return 1;
        }
       
        public Guid UserID
        {
            get
            {
                return userID;
            }
        }
        public string USERNAME
        {
            get
            {
                return userName;
            }
        }
        public string PASSWORD
        {
            get
            {
                return passWord;
            }
        }        
        public string EMail
        {
            get
            {
                return eMail;
            }
        }
    }
}
